#!/bin/bash

# 定义变量
PROJECT_NAME="mysdk"  # 替换为你的项目名称
SCHEME_NAME="mysdk"   # 替换为你的Scheme名称
OUTPUT_DIR="$(pwd)/XCFramework"

# 创建输出目录
mkdir -p "$OUTPUT_DIR"

# 清理构建目录
xcodebuild -project "$PROJECT_NAME.xcodeproj" -scheme "$SCHEME_NAME" -configuration Release clean

# 构建iOS设备框架
xcodebuild -project "$PROJECT_NAME.xcodeproj" -scheme "$SCHEME_NAME" -configuration Release -sdk iphoneos -destination "generic/platform=iOS" ONLY_ACTIVE_ARCH=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES SKIP_INSTALL=NO BUILD_DIR="$(pwd)/build"

# 构建iOS模拟器框架
xcodebuild -project "$PROJECT_NAME.xcodeproj" -scheme "$SCHEME_NAME" -configuration Release -sdk iphonesimulator -destination "generic/platform=iOS Simulator" ONLY_ACTIVE_ARCH=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES SKIP_INSTALL=NO BUILD_DIR="$(pwd)/build"

# 构建macOS框架（可选）
xcodebuild -project "$PROJECT_NAME.xcodeproj" -scheme "$SCHEME_NAME" -configuration Release -sdk macosx -destination "generic/platform=macOS" ONLY_ACTIVE_ARCH=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES SKIP_INSTALL=NO BUILD_DIR="$(pwd)/build"

# 构建watchOS设备框架（可选）
xcodebuild -project "$PROJECT_NAME.xcodeproj" -scheme "$SCHEME_NAME" -configuration Release -sdk watchos -destination "generic/platform=watchOS" ONLY_ACTIVE_ARCH=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES SKIP_INSTALL=NO BUILD_DIR="$(pwd)/build"

# 构建watchOS模拟器框架（可选）
xcodebuild -project "$PROJECT_NAME.xcodeproj" -scheme "$SCHEME_NAME" -configuration Release -sdk watchsimulator -destination "generic/platform=watchOS Simulator" ONLY_ACTIVE_ARCH=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES SKIP_INSTALL=NO BUILD_DIR="$(pwd)/build"

# 构建tvOS设备框架（可选）
xcodebuild -project "$PROJECT_NAME.xcodeproj" -scheme "$SCHEME_NAME" -configuration Release -sdk appletvos -destination "generic/platform=tvOS" ONLY_ACTIVE_ARCH=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES SKIP_INSTALL=NO BUILD_DIR="$(pwd)/build"

# 构建tvOS模拟器框架（可选）
xcodebuild -project "$PROJECT_NAME.xcodeproj" -scheme "$SCHEME_NAME" -configuration Release -sdk appletvsimulator -destination "generic/platform=tvOS Simulator" ONLY_ACTIVE_ARCH=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES SKIP_INSTALL=NO BUILD_DIR="$(pwd)/build"

# 创建XCFramework
xcodebuild -create-xcframework \
    -framework "$(pwd)/build/Release-iphoneos/$PROJECT_NAME.framework" \
    -framework "$(pwd)/build/Release-iphonesimulator/$PROJECT_NAME.framework" \
    -output "$OUTPUT_DIR/$PROJECT_NAME.xcframework"

# 清理临时构建目录
rm -rf "$(pwd)/build"

echo "XCFramework已创建: $OUTPUT_DIR/$PROJECT_NAME.xcframework"

